package com.isc.tcpCommunicationReview;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

public class Client {
    public static void main(String[] args) throws IOException {
        //设置客户端访问服务器的ip、端口
        byte[] addr = new byte[]{127,0,0,1};
        Socket socket = new Socket(InetAddress.getByAddress(addr), 9999);

        //使用字符流发送消息给服务器端
        OutputStream outputStream = socket.getOutputStream();
        //将字节流转换成字符流
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(outputStream));
        bw.write("hello,server~");
        bw.newLine();//结束符，表示信息已经发送完了
        bw.flush();//手动刷新，不然消息发不出去
        System.out.println("客户端发送消息成功...");

        //接收服务器端发送的消息
        InputStream inputStream = socket.getInputStream();
        //将字节流转换成字符流
        BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
        String s = br.readLine();
        System.out.println("服务器端发送的消息是:"+s);

        //关闭资源
        br.close();
        bw.close();
        socket.close();
        System.out.println("与服务器断开连接...");
    }
}
